import { Text, useMessage } from 'alemonjs'
import { selects, validate } from '@src/apps/index'
import { bag } from '@src/db'
import { qidToUid } from '@src/apps/tools'

export const regular = /^(!|！|\/)?(背包|储物袋|灵袋)$/

const response = onResponse(selects, async e => {
  const [message] = useMessage(e)
  const userId = await qidToUid(e.UserId ?? e.UserKey)

  // 查询背包
  const bagList = await bag.findAllValues({ where: { uid: userId } })
  if (!bagList.length) {
    message.send(format(Text('\n空空如也~')))
    return
  }

  // 整理物品列表
  const content = bagList
    .map(item => `[${item.item_name}] × ${item.count}`)
    .join('\n')
  message.send(format(Text(`\n===🎒我的背包🎒===\n${content}`)))
})

export default onResponse(selects, [validate, response.current])
